package com.abc;

import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Rest {
	   // JDBC driver name and database URL
	   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	   static final String DB_URL = "jdbc:mysql://localhost:3306/rest";

	   //  Database credentials
	   static final String USER = "root";
	   static final String PASS = "root";
	   
	   public static void main(String[] args) {
	   Connection conn = null;
	   Statement stmt = null;
	   try{
	      //STEP 2: Register JDBC driver
	      Class.forName("com.mysql.jdbc.Driver");

	      //STEP 3: Open a connection
	      System.out.println("Connecting to a selected database...");
	      conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
	      System.out.println("Connected database successfully...");
	      
	      //STEP 4: Execute a query
	      System.out.println("Creating table in given database...");
	      stmt = (Statement) conn.createStatement();
	      
	      
	      
	      DatabaseMetaData dbmd = conn.getMetaData();  
	      
	      System.out.println("=====  Database info =====bnmool,++");  
	      System.out.println("DatabaseProductName: " + dbmd.getDatabaseProductName() );  
	      System.out.println("DatabaseProductVersion: " + dbmd.getDatabaseProductVersion() );  
	      System.out.println("DatabaseMajorVersion: " + dbmd.getDatabaseMajorVersion() );  
	      System.out.println("DatabaseMinorVersion: " + dbmd.getDatabaseMinorVersion() );  
	      System.out.println("=====  Driver info =====");  
	      System.out.println("DriverName: " + dbmd.getDriverName() );  
	      System.out.println("DriverVersion: " + dbmd.getDriverVersion() );  
	      System.out.println("DriverMajorVersion: " + dbmd.getDriverMajorVersion() );  
	      System.out.println("DriverMinorVersion: " + dbmd.getDriverMinorVersion() ); 
	      
	      String sql = "CREATE TABLE RESTAURANT53  " +
	                   "(id INTEGER not NULL, " +
	                   " first VARCHAR(255), " + 
	                   " place VARCHAR(255), " + 
	                   " price INTEGER, " + 
	                   " PRIMARY KEY ( id ))"; 

	      stmt.executeUpdate(sql);
	      System.out.println("Created table in given database...");
	      
	      
	    
	    	  
	      
	      	String sqlInsert = "insert into RESTAURANT53 values"  +  "(3002,'shariff darbar','rt nagar',1),"
	        + "(3003,'albek','ganga nagar',2)," + "(3004,'punjabi rasoi','jp nagar',5)";
	    	System.out.println("The SQL query is: " + sqlInsert);  							// Echo for debugging
	    	int countInserted = stmt.executeUpdate(sqlInsert);
	    	System.out.println(countInserted + " records inserted.\n");
	    	
	    	 String strUpdate = "update RESTAURANT53 set price = price * 20 where id = 3003";
	         System.out.println("The SQL query is: " + strUpdate);  // Echo for debugging
	         int countUpdated = stmt.executeUpdate(strUpdate);
	         System.out.println(countUpdated + " records affected.");
	         
	    	
	    	 String sqlDelete = "delete from RESTAURANT53  where id=3004";
	         System.out.println("The SQL query is: " + sqlDelete);  // Echo for debugging
	         int countDeleted = stmt.executeUpdate(sqlDelete);
	         System.out.println(countDeleted + " records deleted.\n");
	          
	    	 
	   }catch(SQLException se){
	      //Handle errors for JDBC
	      se.printStackTrace();
	   }catch(Exception e){
	      //Handle errors for Class.forName             
	      e.printStackTrace();
	   }finally{
	      //finally block used to close resources
	      try{
	         if(stmt!=null)
	            conn.close();
	      }catch(SQLException se){
	      }// do nothing
	      try{
	         if(conn!=null)
	            conn.close();
	      }catch(SQLException se){
	         se.printStackTrace();
	      }//end finally try
	   }//end try
	   System.out.println("Goodbye!");
	}//end main
	}//end JDBCExample